﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using Expression.Samples.Interactivity.DataHelpers;

using XamlingCore.SL.Controls.ContentTemplate.Interfaces;

namespace XamlingCore.SL.Controls.ContentTemplate
{
    public class ContentTemplateSelectorBase : IContentTemplateSelector
    {
        private string defaultTemplate;

        Dictionary<Type, string> typeCache = new Dictionary<Type, string>();
        Dictionary<Type, DataTemplate> templateCache = new Dictionary<Type, DataTemplate>();

        public virtual DataTemplate SelectTemplate(object content)
        {
            if (content != null)
            {
                var t = content.GetType();

                if (typeCache.ContainsKey(t))
                {
                    Debug.WriteLine("Type cache template loaded for: " + t.Name);
                    return Application.Current.Resources[typeCache[t]] as DataTemplate;
                }


                var conventionSelection = conventionViewSelection(content);

                if (conventionSelection != null)
                {
                    return conventionSelection;
                }
            }
            if (!string.IsNullOrEmpty(defaultTemplate))
            {
                return Application.Current.Resources[defaultTemplate] as DataTemplate;
            }

            return null;
        }

        //This was lifted from the expression behaviors and modified (expressionblend.codeplex.com)
        DataTemplate conventionViewSelection(object content)
        {

            var t = content.GetType();

            if (templateCache.ContainsKey(t))
            {
                return templateCache[t];
            }

            if (content != null)
            {
                if (t.Name.Contains("ViewModel"))
                {
                    Debug.WriteLine("Generating view template for: " + t.Name);

                    var typeName = t.Name.Replace("ViewModel", "View");
                    var assembly = t.Assembly.FullName;
                    var nameSpace = t.Namespace;

                    StringBuilder stringBuilder = new StringBuilder();
                    stringBuilder.Append("<DataTemplate xmlns='http://schemas.microsoft.com/client/2007' xmlns:c='" +
                                         ("clr-namespace:" + nameSpace + ";assembly=" + assembly.Split(new char[] { ',' })[0]) +
                                         "'>\n");

                    stringBuilder.Append("<c:" + typeName + ">\n");

                    stringBuilder.Append("</c:" + typeName + ">\n");
                    stringBuilder.Append("</DataTemplate>");

                    var instance = XamlReader.Load(stringBuilder.ToString()) as DataTemplate;
                    templateCache.Add(t, instance);
                    return instance;
                }
            }

            return null;
        }


        protected void RegisterType<T>(string templateName)
        {
            typeCache.Add(typeof(T), templateName);
        }

        protected void RegisterDefault(string templateName)
        {
            defaultTemplate = templateName;
        }

    }
}
